 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Employee;

import Address.AddressBean;
import Student.StudentBean;
import User.UserBean;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.Date;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.omg.PortableServer.REQUEST_PROCESSING_POLICY_ID;

/**
 *
 * @author mishra
 */
public class EditEmployee extends HttpServlet {

    HashMap htmlfields = new HashMap();
    UserBean ub = new UserBean();
    AddressBean adb = new AddressBean();
    EmployeeBean eb = new EmployeeBean();
    int i = 0;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Date doj = null;
        HttpSession session = request.getSession();
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        if (isMultipart) {
            try {
                ServletFileUpload servlet = new ServletFileUpload();
                FileItemIterator fit = servlet.getItemIterator(request);
                while (fit.hasNext()) {
                    FileItemStream f = fit.next();
                    if (f.isFormField()) {
                        InputStream input = f.openStream();
                        byte b[] = new byte[input.available()];
                        input.read(b);
                        String value = new String(b);
                        htmlfields.put(f.getFieldName(), value);
                    } else {
                        if (f.getName().isEmpty()) {
                            continue;
                        } else {
                            String fileName = (String) htmlfields.get("email");
                            String postfix = "@photo";
                            String s = DocUpload.FileUpload.writeFile(f, fileName + postfix);
                            eb.setPhoto(s);
                        }
                    }
                }
            } catch (Exception e) {
                session.setAttribute("msg", "Try Again...! Not Done.(" + e + ")");
                session.setAttribute("status", "fail");
                response.sendRedirect("/RIT/Services/HR/HR.jsp");
            }
        }
        try {
            ub.setEmployeeName(htmlfields.get("empName").toString());
            String td[] = htmlfields.get("empDob").toString().split("/");
            if (td.length == 3) {
                ub.setDob(new Date(Integer.parseInt(td[2]) - 1900, Integer.parseInt(td[0]) - 1, Integer.parseInt(td[1])));
            } else {
                ub.setDob(null);
            }
            if (!htmlfields.get("doj").equals("")) {
                SimpleDateFormat formatter1 = new SimpleDateFormat("MM/dd/yyyy");
                java.util.Date d = (java.util.Date) formatter1.parse(htmlfields.get("doj").toString());
                doj = new Date(d.getTime());
            }
            ub.setDor(doj);
            ub.setEmail(htmlfields.get("email").toString());
            ub.setPassword(htmlfields.get("pwd").toString());
            ub.setUserType(htmlfields.get("eType").toString());
            if (htmlfields.get("userStatus").toString().length() != 0) {
                ub.setUserStatus(Integer.parseInt(htmlfields.get("userStatus").toString()));
            } else {
                ub.setUserStatus(0);
            }
            ub.setEntryEmail(session.getAttribute("uid").toString());
            ub.setUserId(Integer.parseInt(htmlfields.get("userId").toString()));
            int i = User.Query.edit(ub);
////--------------------Loging Details part end-------------------------

            adb.setAddress1(htmlfields.get("add1").toString());
            adb.setAddress2(htmlfields.get("add2").toString());
            adb.setCountryId(Integer.parseInt(htmlfields.get("country").toString()));
            adb.setStateId(Integer.parseInt(htmlfields.get("state").toString()));
            adb.setPinCode(htmlfields.get("pin").toString());
            if (htmlfields.get("addid").toString().length() != 0) {
                adb.setAddressId(Integer.parseInt(htmlfields.get("addid").toString()));
            }
            Address.Query.edit(adb);

            eb.setUserId(ub.getUserId());

            eb.setAddressId(adb.getAddressId());
            eb.setDoj(doj);
            eb.setEmployeeFatherName(htmlfields.get("fname").toString());
            eb.setPanCard(htmlfields.get("pcard").toString());
            eb.setMobile(htmlfields.get("mno").toString());
            eb.setPhone(htmlfields.get("pno").toString());
            eb.setGender(htmlfields.get("gender").toString());
            eb.setHigherQuali(htmlfields.get("hquali").toString());
            eb.setJobdetails(htmlfields.get("jdetail").toString());
            if (htmlfields.get("empid").toString().length() != 0) {
                eb.setEmployeeId(Integer.parseInt(htmlfields.get("empid").toString()));
            }
            if (htmlfields.get("department").toString().length() != 0) {
                eb.setDepartmentId(Integer.parseInt(htmlfields.get("department").toString()));
            }
            if (htmlfields.get("designation").toString().length() != 0) {
                eb.setDesignationId(Integer.parseInt(htmlfields.get("designation").toString()));
            }
            if (htmlfields.get("empType").toString().length() != 0) {
                eb.setEmployeeType(Integer.parseInt(htmlfields.get("empType").toString()));
            }
            if (htmlfields.get("empStatus").toString().length() != 0) {
                eb.setEmployeeStatus(Integer.parseInt(htmlfields.get("empStatus").toString()));
            }

            i = Employee.Query.edit(eb);

            if (i != 0) {
                session.setAttribute("msg", "Employee Profile Updated Successfully.");
                session.setAttribute("status", "success");
                response.sendRedirect("/RIT/Services/HR/HR.jsp");
            } else {
                session.setAttribute("msg", "Try Again...! Not Done.");
                session.setAttribute("status", "fail");
                response.sendRedirect("/RIT/Services/HR/HR.jsp");
            }

        } catch (Exception e) {
            session.setAttribute("msg", "Try Again...! Not Done.(" + e + ")");
            session.setAttribute("status", "fail");
            response.sendRedirect("/RIT/Services/HR/HR.jsp");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
